<template>
  <view class="login-box">
    <view>查看token: </view>
    <view class="input-box">
      <u-form :model="form" ref="uForm">
        <u-form-item label="token:" prop="token">
          <u-input class="token-input" v-model="form.token" />
        </u-form-item>
      </u-form>
    </view>
    <u-button type="primary" @click="login">登录</u-button>
  </view>
</template>

<script>
import { mapActions } from "vuex";
import { accesstoken } from "@/api/user.js";
export default {
  data() {
    return {
      form: {
        token: "223bd9c5-7f42-4355-a897-daaf8594c174",
      },
      rules: {
        token: [
          {
            required: true,
            message: "请填写token",
            trigger: ["change"],
          },
        ],
      },
    };
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    ...mapActions("user", ["userInfo_action", "token_action"]),
    login() {
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          //   accesstoken({ accesstoken: this.form.token }).then((res) => {
          //   });
          let obj = {
            success: true,
            loginname: "tianguoyuan",
            avatar_url:
              "https://avatars.githubusercontent.com/u/37494020?v=4&s=120",
            id: "60a0c0f24d20cb6afa6939f1",
          };
          this.userInfo_action(Object.assign(obj, { token: this.form.token }));
          this.token_action(this.form.token);
          uni.showToast({
            icon: "success",
            title: "登陆成功",
            duration: 2000,
          });
          setTimeout(() => {
            console.log(uni.redirectTo);
            uni.switchTab({ url: "/pages/home/index" });
          }, 2000);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login-box {
  padding: 20rpx;
  .token-input {
    border-bottom: 1px solid #ccc;
  }
}
</style>